import urlpyroxy
SERVERTIME = 0
OKXIP = "43.199.52.88"
okxDoMain = "aws.okx.com"
# U本位OKX永续合约资金费率
def getURate(api = 'api/v5/public/funding-rate?instId=BTC-USDT-SWAP'):
    data = urlpyroxy.getURLWithIP(api,OKXIP,okxDoMain)
    if data and data.get("code") == "0" and data.get("data"):
        funding_rate = float(data["data"][0]["fundingRate"])  # 从 JSON 数据中提取 fundingRate
        return funding_rate
    return None
# 币本位OKX永续合约资金费率
def getBRate(api = 'api/v5/public/funding-rate?instId=BTC-USD-SWAP'):
    data = urlpyroxy.getURLWithIP(api,OKXIP,okxDoMain)
    if data and data.get("code") == "0" and data.get("data"):
        funding_rate = float(data["data"][0]["fundingRate"])  # 从 JSON 数据中提取 fundingRate
        return funding_rate
    return None
# OKX精英U本位仓位多空比,前5%
def getMoreV(api = 'api/v5/rubik/stat/contracts/long-short-position-ratio-contract-top-trader',instId = 'BTC-USDT-SWAP',period = '30m',limit = 5):
    api = api + f'?instId={instId}&period={period}&limit={limit}'
    data = urlpyroxy.getURLWithIP(api,OKXIP,okxDoMain)
    if data and data.get("code") == "0" and data.get("data"):
        return data.get("data")[0][1]
    return None
#前5%精英多空比,币本位
def getMoreBV():
    data = getMoreV(instId = 'BTC-USD-SWAP')
    return None
# OKX精英U本位仓位多空比,前5%
def getMoreUV():
    data = getMoreV(instId = 'BTC-USDT-SWAP')
    return None

# 获取okx多空比:https://aws.okx.com/api/v5/rubik/stat/contracts/long-short-account-ratio?ccy=BTC
def getMoreP(api = 'api/v5/rubik/stat/contracts/long-short-account-ratio?ccy=BTC'):
    data = urlpyroxy.getURLWithIP(api,OKXIP,okxDoMain)
    if data and data.get("code") == "0" and data.get("data"):
        return data.get("data")[0][1]
    return None
#https://aws.okx.com/api/v5/rubik/stat/margin/loan-ratio?ccy=BTC
#杠杆融币多空比

#获取系统时间
def getTime(api = 'api/v5/public/time'):
    global SERVERTIME
    data = urlpyroxy.getURLWithIP(api,OKXIP,okxDoMain)
    if data and data.get("code") == "0" and data.get("data"):
        SERVERTIME = int(data.get("data")[0]['ts']/1000) #单位秒
        return SERVERTIME
    return None